package com.hqyj.manager.service.Impl;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hqyj.mapper.CategoryMapper;
import com.hqyj.mapper.ProductImageMapper;
import com.hqyj.mapper.ProductMapper;
import com.hqyj.manager.service.ProductsService;
import com.hqyj.pojo.Category;
import com.hqyj.pojo.Product;
import com.hqyj.pojo.ProductImage;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;

@Service
public class ProductsServiceImpl implements ProductsService {

    @Resource
    private ProductMapper productMapper;
    @Resource
    private CategoryMapper categoryMapper;
    @Resource
    private ProductImageMapper productImageMapper;
    @Override
    public List<Product> getAll() {
        System.out.println(productMapper.selectProductAndProductImage());
        return productMapper.selectProductAndProductImage();
    }



    @Override
    public PageInfo<Product> sreachProduct(Integer pageNum, Integer pageSize, Integer categoryId, Integer reviewCount, String searchStr) {
        PageHelper.startPage(pageNum, pageSize);
        //List<Product> allProduct = productMapper.selectProductAndProductImage();
        List<Product> allProduct = productMapper.findAllProduct(categoryId,reviewCount,searchStr);
        System.out.println("allProduct的数据"+allProduct);
        //pagehelper内部计算总共有多少页
        PageInfo<Product> pageInfo = new PageInfo<>(allProduct);
        return pageInfo;
    }

    @Override
    public PageInfo<Product> findAllProduct(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
       List<Product> allProduct = productMapper.selectProductAndProductImage();
       // List<Product> allProduct = productMapper.findAllProduct(categoryId,reviewCount,searchStr);
        //pagehelper内部计算总共有多少页
        System.out.println("我是计算出来的"+allProduct);
        PageInfo<Product> pageInfo = new PageInfo<>(allProduct);
        return pageInfo;
    }

    /*删除单个*/
    @Override
    public Boolean deleteProductById(Integer id) {
        try {
             productMapper.deleteProductById(id);
             productImageMapper.deleteProductImageById(id);
             return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }

    }

    /*修改状态*/
    @Override
    public boolean update(Integer id) {
        try {
            Product product = null;
            try {
                product = productMapper.selectId(id);
            } catch (Exception e) {
                e.printStackTrace();
            }
            System.out.println("修改前状态："+product.getReviewCount());
            if (product!=null){
                Integer status = product.getReviewCount();
                if (status==0){
                    product.setReviewCount(1);
                }else {
                    product.setReviewCount(0);
                }
                productMapper.update(product);
                System.out.println("修改后状态："+product.getReviewCount());
            }
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    @Override
    public PageInfo<Category> slectCategory(Integer pageNum,Integer pageSize,String sreachCategory) {
        PageHelper.startPage(pageNum, pageSize);
        List<Category> allProduct = categoryMapper.CategoryList(sreachCategory);
        PageInfo<Category> pageInfo = new PageInfo<>(allProduct);
        return pageInfo;
    }

    @Override
    public List<Category> slectCategory() {
        return categoryMapper.selectAll();
    }

    @Override
    public Boolean deleteCategoryById(Integer id) {
        return categoryMapper.deleteCategoryById(id);
    }

    /*/**
    *@author: Administrator
    *@ClassName:ProductsServiceImpl
    *@date: 2020/11/28 0028 14:41
    *@return: java.lang.Boolean
    *@description: 分类增加
    */
    @Override
    public Boolean categoryAdd(String name) {
        System.out.println(name);
        return categoryMapper.categoryAdd(name);
    }

    /*分类批量删除*/
    @Override
    public Integer deleteByIds(String oIds) {
        String[] split = oIds.split(",");
        List<String> oIdsList = Arrays.asList(split);
        Integer integer = categoryMapper.deleteByIds(oIdsList);
        return integer;
    }

    /*/**
    *@author: Administrator
    *@ClassName:ProductsServiceImpl
    *@date: 2020/11/28 0028 14:41
    *@return: java.util.HashMap<java.lang.String,java.lang.Object>
    *@description: 商品增加模块
    */
    @Override
    public HashMap<String, Object> productAdd(Product product, String url1, String url2) {
        HashMap<String, Object> map = new HashMap<>();
        try {
            int insert = productMapper.insert(product);
            if (insert>0){
                map.put("key1","产品内容增加成功");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        try {
            if (url1!=null){
                ProductImage productImage = new ProductImage();
                productImage.setProductId(product.getId());
                productImage.setType(0);
                productImage.setUrl(url1);
                int insert1 = productImageMapper.insert(productImage);
                if (insert1==1){
                    map.put("key2","大图片传送成功");
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        try {
            if (url2!=null){
                String[] split = url2.split(",");
                List<String> list = Arrays.asList(split);
                List<ProductImage> lists = new ArrayList<>();
                System.out.println("我是传进来的图片地址："+list);
                for (int i = 0; i <list.size() ; i++) {
                    ProductImage productImage = new ProductImage();
                    productImage.setProductId(product.getId());
                    productImage.setType(1);
                    productImage.setUrl(list.get(i));
                    lists.add(productImage);
                }
                Integer  integer = productImageMapper.productImageAdd(lists);
                if (integer==lists.size()){
                    map.put("key3","详情页传送成功");
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return map;
    }

    /*/**
    *@author: Administrator
    *@ClassName:ProductsServiceImpl
    *@date: 2020/11/28 0028 14:53
    *@return: java.lang.Integer
    *@description: 批量删除商品
    */
    @Override
    public Boolean deleteProductByIds(String oIds) {
        Integer integer = null;
        Integer integer1=null;
        try {
            String[] split = oIds.split(",");
            List<String> oIdsList = Arrays.asList(split);
            integer = productMapper.deleteProductByIds(oIdsList);
            integer1 = productImageMapper.deleteProductImageByIds(oIdsList);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return integer>0&&integer1>0;
    }

    //查询单个商品
    @Override
    public Product sreachProductById(Integer id) {
        return productMapper.selectId(id);
    }

    //修改商品信息
    @Override
    public HashMap<String, Object> productUpdate(Product product, String url1, String url2) {
        HashMap<String, Object> map = new HashMap<>();
        try {
            productMapper.update(product);
            productImageMapper.updateByUrl(product.getId(),url1);
            map.put("key",true);
        } catch (Exception e) {
            e.printStackTrace();
            map.put("key",false);
        }

        return map;
    }

    @Override
    public Category sreachCategory(Integer id) {
        return categoryMapper.selectById(id);
    }

    @Override
    public HashMap<String, Object> categoryUpdate(Category category) {
        HashMap<String, Object> map = null;
        try {
            map = new HashMap<>();
            Boolean aBoolean = categoryMapper.categoryUpdate(category.getId(),category.getName());
            if (aBoolean==true){
                map.put("key",true);
            }else {
                map.put("key","有问题");
            }
        } catch (Exception e) {
            map.put("key","出现错误");
            e.printStackTrace();
        }
        return map;
    }

    //生成exel格式文档
    @Override
    public void excelWrite(HttpServletResponse response) {
/*
        OutputStream output=null;
        try {
            // 创建HSSFWorkbook对象
            HSSFWorkbook wb = new HSSFWorkbook();
            // 创建HSSFSheet对象,如果要创建多个sheet，就再创建sheet对象
            HSSFSheet sheet = wb.createSheet("商品表");
            //data日期类型转换为string类型
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
            // 创建HSSFRow对象，先写入列名
            HSSFRow colName = sheet.createRow(0);
            // 写入入第一行列名
            colName.createCell(0).setCellValue("编号");
            colName.createCell(1).setCellValue("登录名");
            colName.createCell(2).setCellValue("手机");
            colName.createCell(3).setCellValue("邮箱");
            colName.createCell(4).setCellValue("角色");
            colName.createCell(5).setCellValue("加入时间");
            colName.createCell(6).setCellValue("状态");
            colName.createCell(7).setCellValue("密码");
            colName.createCell(8).setCellValue("头像地址");
            //查询分页参数对象
            PageRequest pageable = PageRequest.of(0, 10);
            List<UserInfo> list = userInfoDao.findAll(pageable).getContent();
            // List<UserInfo> list = userInfoDao.findAll();
            for (int i = 1; i <=list.size(); i++) {
                //从第二行开始写入数据
                HSSFRow row = sheet.createRow(i);
                row.createCell(0).setCellValue(list.get(i-1).getuId());
                row.createCell(1).setCellValue(list.get(i-1).getUserName());
                row.createCell(2).setCellValue(list.get(i-1).getUserDetail().getTel());
                row.createCell(3).setCellValue(list.get(i-1).getUserDetail().getEmail());
                String roleName="";
                if(list.get(i-1).getRoleInfoList()!=null){
                    for(RoleInfo r :list.get(i-1).getRoleInfoList()){
                        roleName+=r.getrName()+"";
                    }
                }
                row.createCell(4).setCellValue(roleName);
                row.createCell(5).setCellValue(dateFormat.format(list.get(i-1).getUserDetail().getJoinTime()));
                row.createCell(6).setCellValue(list.get(i-1).getUserDetail().getStatus());
                row.createCell(7).setCellValue(list.get(i-1).getUserPwd());
                row.createCell(8).setCellValue(list.get(i-1).getUserDetail().getUrl());


            }
}*/
    }
}
